package com.lzp.customview.adsorption

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import androidx.core.view.ViewCompat
import androidx.customview.widget.ViewDragHelper
import com.utils.SizeUtils

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/9/3
 */
class AdsorptionButton : FrameLayout {

    private val TAG = "AdsorptionButton"

    private val dragHelper = ViewDragHelper.create(this, DragCallBack())

    private var leftAndRightMargin = 0

    private var moveView: View? = null
    private val locationArr = IntArray(2)

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        leftAndRightMargin = SizeUtils.dip2px(context, 0)
    }


    override fun computeScroll() {
        if (dragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this)
        }
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            return dragHelper.shouldInterceptTouchEvent(it)
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (moveView == null) {
            return super.dispatchTouchEvent(ev)
        }
        val moveViewTemp = moveView!!
        ev?.let {
            moveViewTemp.getLocationInWindow(locationArr)
            return when (it.action) {
                MotionEvent.ACTION_DOWN -> {
                    if (isTouchOnView(moveViewTemp,it)) {
                        dragHelper.processTouchEvent(it)
                        true
                    } else {
                        super.dispatchTouchEvent(ev)
                    }
                }
                else -> {
                    super.dispatchTouchEvent(ev)
                }
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    private fun isTouchOnView(view: View, ev: MotionEvent): Boolean {
        val x = ev.rawX
        val y = ev.rawY

        val left = locationArr[0]
        val top = locationArr[1]
        val right = left + view.measuredWidth
        val bottom = top + view.measuredHeight

        return y >= top && y <= bottom && x >= left && x <= right
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        if (childCount == 2) {
            moveView = getChildAt(1)
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        event?.let {
            dragHelper.processTouchEvent(it)
            return true
        }
        return super.onTouchEvent(event)
    }

    private inner class DragCallBack : ViewDragHelper.Callback() {
        override fun tryCaptureView(child: View, pointerId: Int): Boolean {
            return child == moveView
        }

        override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int {
            var tempLeft = left
            if (left < 0) {
                tempLeft = 0
            } else if (left > measuredWidth - (moveView?.measuredWidth ?: 0)) {
                tempLeft = measuredWidth - (moveView?.measuredWidth ?: 0)
            }
            return tempLeft
        }

        override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int {
            var tempTop = top
            if (tempTop < 0) {
                tempTop = 0
            } else if (tempTop > measuredHeight - (moveView?.measuredHeight ?: 0)) {
                tempTop = measuredHeight - (moveView?.measuredHeight ?: 0)
            }
            return tempTop
        }

        override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) {
            val left = releasedChild.left
            val top = releasedChild.top
            if (left > (measuredWidth - releasedChild.measuredWidth) / 2 - leftAndRightMargin) {
                dragHelper.smoothSlideViewTo(releasedChild, measuredWidth - releasedChild.measuredWidth - leftAndRightMargin, top)
                ViewCompat.postInvalidateOnAnimation(this@AdsorptionButton)
            } else {
                dragHelper.smoothSlideViewTo(releasedChild, leftAndRightMargin, top)
                ViewCompat.postInvalidateOnAnimation(this@AdsorptionButton)
            }
        }
    }

}